<?php

class MilestonesController extends Controller
{
	public function actionIndex()
	{
		$pid = Yii::app()->request->getParam("pid");
        // get all schedules associate with project
        $modelSchedules = Schedule::model()->findAllByAttributes(array('schedule_project_id' => $pid, 'schedule_type' => "MILESTONE"));
        
		$schedules = array();
		foreach ($modelSchedules as $modelSchedule) {
			$modelMilestone  = Milestone::model()->findByAttributes(array('milestone_schedule_id' => $modelSchedule->schedule_id));
			$modelMilestoneUsers = MilestoneUser::model()->findAllByAttributes(array('milestone_user_milestone_id' => $modelMilestone->milestone_id));
			$schedule['modelSchedule'] = $modelSchedule;
			$schedule['modelMilestone'] = $modelMilestone;
			$schedule['modelMilestoneUsers'] = $modelMilestoneUsers;
			$schedules[] = $schedule;
		}
		$milestone = new Milestone();
		$schedule = new Schedule();
		$this->render('index', array('schedules' => $schedules, 'schedule' => $schedule, 'milestone' => $milestone));
	}

	// -----------------------------------------------------------
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
	public function actionAdd()
	{
		$pid = Yii::app()->request->getParam('pid');
		if($pid == null) throw new CHttpException(404,'Param not enough');
		
		$milestone = new Milestone();
		$schedule = new Schedule();
		if(!isset($_POST['Milestone']) || !isset($_POST['Schedule'])) {
			$this->render('add', array('milestone' => $milestone, 'schedule' => $schedule));
			return;
		}
		$milestone->attributes = $_POST['Milestone']; 
		$schedule->attributes = $_POST['Schedule'];
		$schedule->schedule_created_date = new CDbExpression('NOW()');
		$schedule->schedule_type = 'MILESTONE';
		$schedule->schedule_project_id = $pid;
		$schedule->schedule_created_by_user_id = Yii::app()->user->id; 
		
		if($schedule->save()) {
			$milestone->milestone_schedule_id = $schedule->schedule_id;
			if(isset($_POST['milestone_notify']))
				$milestone->milestone_notify = 1;
			else
				$milestone->milestone_notify = 0;
			if(isset($_POST['milestone_reminder']))
				$milestone->milestone_reminder = 1;
			else
				$milestone->milestone_reminder = 0;
			if($milestone->save()) {
				// Save milestone user
				if (isset($_POST['milestone_user']))
					$userid = $_POST['milestone_user'];
					$milestone_user = new MilestoneUser();
					$milestone_user->milestone_user_milestone_id = $milestone->milestone_id;
					$milestone_user->milestone_user_responsible_user_id = $userid;
					$milestone_user->save();
				if(isset($_POST['milestone_user_more']))
					foreach($_POST['milestone_user_more'] as $userid) {
						$userid = $_POST['milestone_user'];
						$milestone_user = new MilestoneUser();
						$milestone_user->milestone_user_milestone_id = $milestone->milestone_id;
						$milestone_user->milestone_user_responsible_user_id = $userid;
						$milestone_user->save();
					}
					
				//$this->sendMail($meeting, $_POST['meeting_user']);
				Yii::app()->user->setFlash('success','Add milestone successfully');
				$this->redirect(array('/milestones/index','pid'=>$pid));				
			}
		}
		//$this->render('add', array('milestone' => $milestone, 'schedule' => $schedule));
	}
}